﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using esri.DataBase;

namespace esri
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //设置textbox的显示内容
        public bool flag = false;//判断是否连接了数据库
        public void SetText(int i,string str)
        {
            switch (i)
            {
                case 1: textBox1.Text = str; break;
                case 2: textBox2.Text = str; break;
                case 3: textBox3.Text = str; break;
                case 4: textBox4.Text = str; break;
            }
            
        }

        //初始化form1 
        public void Initialize()
        {
            comboBox1.Items.Add("Windows身份验证");
            comboBox1.Items.Add("SQL身份验证");

            //提前显示的部分字符串内容
            SetText(1, System.Net.Dns.GetHostName());
            SetText(2, "GPSDBNew");
            SetText(3, "EAGLE");
            SetText(4, "12345");//（改成**）

        }

        //响应“连接数据库”按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            //新建一个实例，数据库操作
            DBOperation DBO = new DBOperation();
            

            try
            {
                //windows方式连接数据库
                if (comboBox1.SelectedItem.Equals("Windows身份验证"))
                {
                    if (textBox2.Text == null)
                        MessageBox.Show("请输入完整信息！");
                    else
                    {
                        DBConnection DBC = DBConnection.Instance();
                        DBC.HostName = textBox1.Text;

                        DBC.DBName = textBox2.Text;

                        DBO.SetConnString(DBC.HostName, DBC.DBName);
                        try
                        {
                            DBO.Connect();
                            MessageBox.Show("数据库连接成功！");
                            DBC.ConnectionString = DBO.ConnectionString;
                            flag = true;
                            this.Close();
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show("数据库连接错误！请重试！");
                        }
                    }
                }
                //以SQL方式连接数据库
                else
                {
                    if (textBox2.Text == null || textBox3.Text == null || textBox4.Text == null)
                        MessageBox.Show("请输入完整信息！");
                    else
                    {
                        DBConnection DBC = DBConnection.Instance();
                        DBC.HostName = System.Net.Dns.GetHostName();
                        DBC.DBName = textBox2.Text;
                        DBC.UserID = textBox3.Text;
                        DBC.Password = textBox4.Text;

                        DBO.SetConnString(DBC.HostName, DBC.DBName, DBC.UserID, DBC.Password);
                        try
                        {
                            DBO.Connect();
                            MessageBox.Show("数据库连接成功！");
                            DBC.ConnectionString = DBO.ConnectionString;
                            flag = true;
                            this.Close();
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show("数据库连接错误！请重试！");
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("请选择数据库连接方式！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.Equals("Windows身份验证"))
            {
                textBox3.Enabled = false;
                textBox4.Enabled = false;
            }
           if (comboBox1.SelectedItem.Equals("SQL身份验证"))
            {
                textBox3.Enabled = true;
                textBox4.Enabled = true;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!flag)
            {
                Application.Exit();
            }
        }

        
    }
}
